package cn.tedu.school.portal.controller;

import cn.tedu.school.portal.service.ex.*;
import cn.tedu.school.portal.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一处理异常的类
 */
@Slf4j
@RestControllerAdvice  //该类中处理异常的方法可以作用于整个项目
public class GlobalExceptionHandler {


    @ExceptionHandler
    public R handleException(Throwable e) {
        if(e instanceof ParameterValidationException){
            return R.failure(R.State.ERR_PARAMETER_VALIDATION,e);
        }else if(e instanceof StudentIdDuplicateException){
            return R.failure(R.State.ERR_STUDENT_ID_DUPLICATE,e);
        }else if(e instanceof InsertException){
            return R.failure(R.State.ERR_INSERT,e);
        }else if(e instanceof StudentDoNotExistException){
            return R.failure(R.State.ERR_STUDENT_DO_NOT_EXIST,e);
        }else if(e instanceof PasswordNotMatchException){
            return R.failure(R.State.ERR_PASSWORD_NOT_MATCH,e);
        }else if(e instanceof PasswordDuplicateException){
            return R.failure(R.State.ERR_PASSWORD_DUPLICATE,e);
        } else{
            log.debug("UnknownException> {} type>{}",e.getClass(),e);
            return R.failure(R.State.ERR_UNKNOWN,e);
        }
    }

}
